package com.weimei.conversionTreasure.viewModel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.weimei.conversionTreasure.activity.VipActivity;
import com.weimei.conversionTreasure.http.entity.BaseResult;
import com.weimei.conversionTreasure.utils.UserUtils;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class AddMarketVM extends TitleVM {

    public ObservableField<String> id = new ObservableField<>("");


    public AddMarketVM(@NonNull Application application, BaseModel model) {
        super(application, model);
    }


    public void saveData(String type, String text, String edtMakerText, String className) {
        if (!UserUtils.isVip()){
            ToastUtils.showShort("您还不是会员，请先开通会员后再使用！！！");
            startActivity(VipActivity.class);
            return;
        }


        Map<String, String> map = new HashMap<>();
        map.put("user_id", UserUtils.getUserAccount());
        map.put("types", type);
        map.put("className", className);
        map.put("notes", edtMakerText);
        map.put("amount", text);
        map.put("customID", id.get());
        model.add_balance(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {
                })
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult baseResult) {
                        if (baseResult.issucc) {
                            ToastUtils.showShort(baseResult.msg);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
